//*****************************************************************************
//
//
//*****************************************************************************

#include "compilCondition.h"
#include "data_struct.h"
#include "pointMotion.h"
#include "peripherial_Init.h"

#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif

unsigned char drawTbl[192] = {0};			//the arry content is drawn every pass at the screen
point_t points[3];							//arry of point structures
moveLaw_t moveLaws[8];						//all move directions are writen here
unsigned short sysFlags = 0;				//bit0 -> ADC change ; bit1 -> Timer 1 passed
int ADCvalue = 0;							//ceepeng the last vlid ADC value
unsigned int timerCounter = 1;				//counting the timer cycles
unsigned int timerCounterMax = 0;	 		//the minimal value that is devideble of speeds of all points
unsigned char overlowStr[6] = " 00%\r";


void ADCIntHandler(void)
{
  
    unsigned long newADCValue=0;
	static short pascount = 0;
    //
    // Clear the ADC interrupt.
    //
    ADCIntClear(ADC_BASE, 3);
	pascount++;
    //
    // Read the data from the ADC.
    //
    //ADCSequenceDataGet(ADC_BASE, 0, &ulData);
 //      while(!ADCIntStatus(ADC_BASE, 0, false))
//            {
//           
//            }
	if(pascount > 5){
	    ADCSequenceDataGet(ADC_BASE, 3, &newADCValue);
	    if( ( (newADCValue - ADCvalue) >40 ) || ( (ADCvalue - newADCValue) >40 ) )
	    {
	        ADCvalue = (int)newADCValue;
	//	    if( sysFlags&0x01 == 0){
	       		sysFlags ^= 0x01;
	//		}
	    }
	}else if( pascount==6 ){
		pascount = 0;
	}
    //ADCProcessorTrigger(ADC_BASE, 0);
	
}

void
Timer0IntHandler(void)
{
    //
    // Clear the timer interrupt.
    //
    TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);

    //
    // Increment the tmer counter (needed for applying speed for the points)
    //
    
	sysFlags ^= 0x02;

	timerCounter += 1;

	if(timerCounter > timerCounterMax){	  
		timerCounter = 1;
	}

}

void convertToOverflow(void)
{
    char num[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
	int overflow = ((ADCvalue - 512)*100)/1023;

	if( overflow == 0){
		overlowStr[0] = ' ';
		overlowStr[2] = num[overflow%10];
    	overlowStr[1] = num[overflow/10];
		return;

	}

	if( overflow > 0){
		overlowStr[0] = '+';
	}else {
		overlowStr[0] = '-';
		overflow *= -1;
	}
    overlowStr[2] = num[overflow%10];
    overlowStr[1] = num[overflow/10];
	
}

#ifdef UART_ENABLE
void
UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{
    while(ulCount--)
    {
        // Write the next character to the UART.
        UARTCharPutNonBlocking(UART0_BASE, *pucBuffer++);
    }
}  
#endif

int
main(void)
{

#ifdef PROGRAM_LENGHT_MEASURE
	unsigned long beginTime = 0;
	unsigned long endTime = 0;
	unsigned long procesTime = 0;
#endif

	unsigned long newTimerLoadSet;


	peripherialInit();

	moveLaws[0].aplToX =  0; moveLaws[0].aplToY = +1;		//Posoka 1
	moveLaws[1].aplToX =  0; moveLaws[1].aplToY = -1;		//Posoka 2
	moveLaws[2].aplToX = +1; moveLaws[2].aplToY =  0;		//Posoka 3
	moveLaws[3].aplToX = -1; moveLaws[3].aplToY =  0;		//Posoka 4
	moveLaws[4].aplToX = +1; moveLaws[4].aplToY = +1;		//Posoka 5
	moveLaws[5].aplToX = -1; moveLaws[5].aplToY = -1;		//Posoka 6
	moveLaws[6].aplToX = -1; moveLaws[6].aplToY = +1;		//Posoka 7
	moveLaws[7].aplToX = +1; moveLaws[7].aplToY = -1;		//Posoka 8


	pointsInit();

#ifdef UART_ENABLE
	UARTSend("Overflow:\r\n",11);
#endif

	for EVER
	{

#ifdef SYS_TIMER0_32B_ENABLE
	while( (sysFlags&0x02) == 0 );					//wait for the timer sinc

	sysFlags ^= 0x02;								//reset the flag for the next wait
#endif

#ifdef MEASURE_SET1
	points[0].x = 90;
	points[0].y = 0;
	points[0].move = &moveLaws[5];
	points[0].speed = 3;
	
	points[1].x = 15;
	points[1].y = 6;
	points[1].move = &moveLaws[4];
	points[1].speed = 4;
	
	points[2].x = 16;
	points[2].y = 7;
	points[2].move = &moveLaws[5];
	points[2].speed = 5;

#endif

#ifdef MEASURE_SET2
	points[0].x = 16;
	points[0].y = 6;
	points[0].move = &moveLaws[6];
	points[0].speed = 3;
	
	points[1].x = 15;
	points[1].y = 6;
	points[1].move = &moveLaws[4];
	points[1].speed = 4;
	
	points[2].x = 16;
	points[2].y = 7;
	points[2].move = &moveLaws[5];
	points[2].speed = 5;

#endif

#ifdef MEASURE_SET3
	points[0].x = 90;
	points[0].y = 0;
	points[0].move = &moveLaws[5];
	points[0].speed = 3;
	
	points[1].x = 0;
	points[1].y = 6;
	points[1].move = &moveLaws[6];
	points[1].speed = 4;
		
	points[2].x = 95;
	points[2].y = 15;
	points[2].move = &moveLaws[4];
	points[2].speed = 5;

#endif	
#ifdef PROGRAM_LENGHT_MEASURE
	beginTime = TimerValueGet( TIMER2_BASE, TIMER_A );
#endif
	
	if( sysFlags & 0x01 )							//check if there is change in the ADC value
	{
		sysFlags ^= 0x01;
		
		newTimerLoadSet = baseProgramCycleTime + (((signed long)baseProgramCycleTime*(ADCvalue-512))/1023);
		//apply new time to the timer
		TimerLoadSet(TIMER0_BASE, TIMER_A, newTimerLoadSet );

#ifdef UART_ENABLE
		convertToOverflow();
		UARTSend(overlowStr,6);
#endif
	}

	pointMove();									//Do the moving procedurs on the points

#ifdef PROGRAM_LENGHT_MEASURE_CALC
	endTime = TimerValueGet( TIMER2_BASE, TIMER_A );
	procesTime = beginTime - endTime;
	endTime = procesTime;
#endif

	IntMasterDisable();
	OSRAM96x16x1ImageDraw(drawTbl, 0, 0, 96, 2);	//draw the new positions on the display
	IntMasterEnable();

#ifdef PROGRAM_LENGHT_MEASURE_ALL
	endTime = TimerValueGet( TIMER2_BASE, TIMER_A );
	procesTime = beginTime - endTime;
	endTime = procesTime;
#endif

	


	}
}
